<?php
/**
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */

include 'include.php';
@error_reporting(E_WARNING);

//buscamos si existen variables get y las removemos

extract($_SERVER);
$modules = substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1);
if ($modules != ""){
    
    $url_param = explode("/",substr(str_replace($SCRIPT_NAME,"",$PHP_SELF),1));    
    $className = ucfirst($url_param[VeCmsEnum::MODULE])."Controller";
    if (!class_exists($className)){
        throw new Exception("Error No existe el controlador indicado");
    }
    else{
        $myModule = new $className;
        $methodName = 'action'.ucfirst($url_param[1]);
        if (!method_exists($myModule, $methodName)){
            throw new Exception("Error no se encontro la accion indicada");
        }
        else{
            call_user_func(array($myModule,$methodName),$_GET);
            $myModule->render($url_param[0],$url_param[1]);
        }
    }    
}
else{
    
    list($class_name,$method_name)=  each($configure["module_default"]);
    $classInstance = $class_name."Controller";
    $methodName = "action".ucfirst($method_name);
    
    //verificamos que el controlador exista
    
    if (!class_exists($classInstance)){
        throw new Exception("Error no se encontro el controlador por defecto");
    }
    else{
        $myModule = new $classInstance;
        
        if (!method_exists($myModule, $methodName)){
            throw new Exception("Error no se encontro la la accion indicada");
        }
        else{
            call_user_func(array($myModule,$methodName),$_GET);
            $myModule->render($class_name,$method_name);
        }
    }
}
?>
